package com.googlecode.gaelloon.routing

import HttpMethod._

case class DispatchAction(action: String) extends Action

trait RouteConfig {

  abstract class MethodWithPath(path: String)

  case class GET(path: String) extends MethodWithPath(path)

  case class POST(path: String) extends MethodWithPath(path)

  case class PUT(path: String) extends MethodWithPath(path)

  case class DELETE(path: String) extends MethodWithPath(path)

  private var routes: List[(HttpMethod, String, Action)] = Nil

  def ::(route: (MethodWithPath, String)) {
    val (mp, action) = route
    val (method, path) = methodWithPath(mp)
    routes = (method, path, DispatchAction(action)) :: routes
  }

  private def methodWithPath(mp: MethodWithPath): (HttpMethod, String) =
    mp match {
      case GET(path) => (HttpMethod.GET, path)
      case POST(path) => (HttpMethod.POST, path)
      case PUT(path) => (HttpMethod.PUT, path)
      case DELETE(path) => (HttpMethod.DELETE, path)
    }

  def build: Seq[(HttpMethod, String, Action)] = routes.reverse
}